package com.demo2do.fms.admin.web.controller.authority.infrastructure;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.authority.VendorSearch;
import com.demo2do.fms.admin.service.DataService;
import com.demo2do.fms.admin.service.authority.OrganizationService;
import com.demo2do.fms.entity.vendor.Vendor;

/**
 * 一般供方，来自Epp
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/vendors")
@MenuSetting("menu-authority")
public class VendorController {
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Vendor> vendorService;
    
    @Autowired
    private OrganizationService organizationService;

    /**
     * 一般供方列表
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-general-supplier")
    public ModelAndView list(Page page) {

        ModelAndView modelAndView = new ModelAndView("authority/infrastructure/vendor/vendor-list");

        int totals = vendorService.count(Vendor.class);
        if(totals > 0){
            
            List<Vendor> vendors = vendorService.listByPage(Vendor.class, page.with(totals));
            
            modelAndView.addObject("vendors", vendors);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }

    /**
     * 查询panel
     *
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-general-supplier")
    public ModelAndView search() {
        ModelAndView modelAndView = new ModelAndView("authority/infrastructure/vendor/vendor-search");
        return modelAndView;
    }

    /**
     * 查询
     * 
     * @param vendorSearch
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-general-supplier")
    public ModelAndView onSearch(VendorSearch vendorSearch, HttpServletRequest request, Page page) {

        ModelAndView modelAndView = new ModelAndView("authority/infrastructure/vendor/vendor-list");

        int totals = vendorService.count(Vendor.class, vendorSearch.getFilter());
        if(totals > 0){
            
            List<Vendor> vendors = vendorService.search(Vendor.class, vendorSearch, page.with(totals));
            modelAndView.addObject("vendors", vendors);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
    }
    
    /**
     * 详情
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-general-supplier")
    public ModelAndView view(@PathVariable("id") String id) {

        ModelAndView modelAndView = new ModelAndView("authority/infrastructure/vendor/vendor-view");
        
        Vendor vendor = organizationService.load(id);
        
        modelAndView.addObject("vendor", vendor);

        return modelAndView;
        
    }

}
